/*
 * @Descripttion: 
 * @version: 
 * @Author: dudu
 * @Date: 2020-08-12 10:06:08
 * @LastEditors: dudu
 * @LastEditTime: 2020-08-13 18:58:01
 */
import { AutoComplete, Input, Select, DatePicker } from './packages'

export const TYPES = {
  AUTO_COMPLETE: 'autocomplete',
  SELECT: 'select',
  INPUT: 'input',
  DATE_PICKER: 'datepicker',
  SELECT_WITH_INPUT: 'selectWithInput'
}

export const typeToComponentMapping = {
  [TYPES.AUTO_COMPLETE]: AutoComplete,
  [TYPES.INPUT]: Input,
  [TYPES.SELECT]: Select,
  [TYPES.DATE_PICKER]: DatePicker,
}

export const createComponentHelper = {
  autocomplete: ({
    key,
    label,
    querySearchAsync,
    placeholder = '',
    tip = '',
    rules,
    required = false
  } = {}) => ({
    type: TYPES.AUTO_COMPLETE,
    key,
    label,
    querySearchAsync,
    placeholder,
    rules,
    tip,
    required
  })
  ,
  select: ({
    key,
    label,
    options = [],
    placeholder = '',
    rules,
    tip = '',
    required = false
  } = {}) => ({
    type: TYPES.SELECT,
    key,
    label,
    options,
    placeholder,
    rules,
    tip,
    required
  }),
  input: ({
    key,
    label,
    reg,
    rules,
    placeholder = '',
    tip = '',
    required = false
  } = {}) => ({
    type: TYPES.INPUT,
    key,
    label,
    reg,
    rules,
    placeholder,
    tip,
    required
  }),
  datepicker: ({
    key,
    label,
    rules,
    tip = '',
    required = false
  } = {}) => ({
    type: TYPES.DATE_PICKER,
    key,
    label,
    rules,
    tip,
    required
  }),
  selectWithInput: ({
    key,
    label,
    children,
    tip = '',
    required = false,
  } = {}) => {
    const disposedChildren = (children || []).map(child => {
      if (!Object.values(TYPES).includes(child.type)) {
        console.error('type matching error')
        return null
      }
      return createComponentHelper[child.type]({
        key,
        label,
        ...child
      })
    })
    return {
      type: TYPES.SELECT_WITH_INPUT,
      key,
      label,
      children: disposedChildren,
      tip,
      required
    }
  }
}




